<?php
	class MyAccount extends CI_Controller {
	   
       function __construct() {
	       parent::__construct();
           $user = $this->session->userdata('user');
           if (!$user) redirect('main/alogin');
           $this->load->model('users/users_db','Users');
           $this->load->model('Dataset_db');
	   }
       
       function changepassword() {
            $user = $this->session->userdata('user');
            $data["userid"] = $user['userid'];
            $data['ptitle'] = "Change Password";
            //print_object($_POST); exit;
            if($_POST['btnChangePassword']) {
                //print_object($_POST); exit;
                $this->form_validation->set_rules('oldpassword', 'Old Password', 'trim|required|max_length[20]');
                $this->form_validation->set_rules('password', 'New Password', 'trim|required|min_length[6]|max_length[20]|matches[cpassword]');
                $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|min_length[6]|max_length[20]');
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                //print_object($_POST); exit;
                $oldpswd = $this->input->post("oldpassword",true);
                $newpswd = $this->input->post("password",true);
                
                if(!$this->Users->is_old_password($user['userid'], $oldpswd)) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Wrong Current Password.");
                    else
                        $data["errors"] = array("Wrong Current Password.");
                }
                
                if($oldpswd==$newpswd) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Current and New Password are same.");
                    else
                        $data["errors"] = array("Current and New Password are same.");
                }
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $pdata = array();
                    $pdata["password"] = md5($newpswd);
                    
                    $save_result = $this->Users->save_user($pdata, $user['userid']);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Password changed successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                        
            $navResult = $this->Dataset_db->buildNav();
            $data['rolelist'] = $this->Users->get_roles();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('changepassword',$data);
       }
       
    }